﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using FrienDev.BusinessRules;
using FrienDev.Data;
using System.Drawing;

public partial class Controls_Club_MemberListDetail : System.Web.UI.UserControl
{

    private static int clubid = -1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitPage();
        }
    }

    private void InitPage()
    {
        if (HttpContext.Current.Request["clubId"] != null)
        {
            clubid = Convert.ToInt32(HttpContext.Current.Request["clubId"]);
        }
        if (clubid != -1)
        {
            this.GridView1.DataSource = ClubRules.Instance.GetMemberListByClubId(clubid, -1);
            this.GridView1.DataBind();
            clubName2.Text = "俱乐部：" + ClubRules.Instance.GetClubByClubId(clubid).ClubName;
            int temp = ClubRules.Instance.GetMemberTypeIdByUserIdAndClubId(clubid, UserRules.Instance.GetCurrentUserId());
            if (temp >= ClubRules.m_3LevelMemberTypeId || temp == -1)
            {
                GridView1.Columns[0].Visible = false;
                buttonsPanel.Visible = false;
            }
            //memberLink.NavigateUrl = "/Club/MemberListDetail.aspx?clubid=" + clubid;
            beMemberLink.NavigateUrl = "/Club/ToBeMemberList.aspx?clubid=" + clubid;
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            User user = e.Row.DataItem as User;
            HyperLink link = e.Row.Cells[2].FindControl("userName") as HyperLink;
            link.Text = user.FullName;
            link.NavigateUrl = ApiHelper.Instance.GetUserHomepageLink(user.UserId);
            System.Web.UI.WebControls.Image img = e.Row.Cells[1].FindControl("headImg") as System.Web.UI.WebControls.Image;
            CheckBox cb = e.Row.Cells[0].FindControl("select") as CheckBox;
            cb.CssClass = user.UserId;
            img.ImageUrl = ApiHelper.Instance.GetUserLogoUrl(user.UserId);
            if (user.City != null)
            {
                e.Row.Cells[4].Text = user.City.CityName;
            }
            else
            {
                e.Row.Cells[4].Text = "未填写";
            }
            string temp = ClubRules.Instance.GetRoleByClubIdAndUserId(clubid, user.UserId);
            if(temp == "主席")
            {
                e.Row.Cells[3].ForeColor = Color.OrangeRed;
            }
            e.Row.Cells[3].Text = temp;
            if(user.LastLoginDateTime.HasValue)
            {
                e.Row.Cells[5].Text = user.LastLoginDateTime.Value.ToString("MM-dd HH:mm");
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //string ids = "";
        //for (int i = 0; i < GridView1.Rows.Count; i++)
        //{
        //    CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("select");
        //    if (cb.Checked)
        //    {
        //        ids += "," + cb.CssClass;
        //    }
        //}
        //if (ids.Length > 0)
        //{
        //    ids = ids.Substring(1);
        string[] ids = GetUsers();
        if (ids != null)
        {
            ClubRules.Instance.UpgradeClubMemberByUserIdAndClubId(ids, clubid);
            InitPage();
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        //string ids = "";
        //for (int i = 0; i < GridView1.Rows.Count; i++)
        //{
        //    CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("select");
        //    if (cb.Checked)
        //    {
        //        ids += "," + cb.CssClass;
        //    }
        //}
        //if (ids.Length > 0)
        //{
        //    ids = ids.Substring(1);
        string[] ids = GetUsers();
        if (ids != null)
        {
            ClubRules.Instance.DegradeClubMemberByUserIdAndClubId(ids, clubid);
            InitPage();
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        string[] ids = GetUsers();
        if(ids != null)
        {
            ClubRules.Instance.KickOutMemberByUserIdAndClubId(ids, clubid);
            InitPage();
        }
    }

    private string[] GetUsers()
    {
        string ids = "";
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("select");
            if (cb.Checked)
            {
                ids += "," + cb.CssClass;
            }
        }
        if (ids.Length > 0)
        {
            ids = ids.Substring(1);
            return ids.Split(',');
        }
        return null;
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        this.GridView1.DataSource = ClubRules.Instance.GetMemberListByClubId(clubid, -1);
        GridView1.DataBind();
    }
}
